package balance

import (
    "errors"
    "math/rand"
)

// 初始化就将该负载均衡算法的实例注册到manager里
func init()  {
    RegisterBalance("random", &RandomBalance{})
}

type RandomBalance struct {
}

// 实现IBalance的接口
func (p *RandomBalance) DoBalance(insts [] *Instance, key...string) (inst *Instance, err error) {
    if len(insts) == 0 {
        err = errors.New("no instance")
        return
    }
    lens := len(insts)
    index := rand.Intn(lens)
    inst = insts[index]
    return
}